<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('科目余额列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" shiro:hasPermission="system:nzjz:nzzz" onclick="doNzzz()"><i class="fa fa-refresh"></i>&nbsp;转账</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
               
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:nzjz:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/nzjz";

        $(function() {
            var options = {
                url: prefix + "/list",
                exportUrl: prefix + "/export",
                modalName: "科目余额",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'kmdm',
                    title: '科目代码',
                },{
                    field: 'kmmc',
                    title: '科目名称',
                }, 
                {
                    field: 'zy',
                    title: '摘要'
                }, 
                {
                    field: 'kmlx',
                    title: '转出方向',
		        	formatter: function (value, row, index) {
		        		return statusTools(row);
		        	}
                },
                {
                    field: 'ye',
                    title: '余额'
                },
                {
                    field: 'zzbl',
                    title: '转入比例'
                }, {
                    title: '科目选择',
                    align: 'center',
                    align: 'left',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info  btn-xs" href="javascript:void(0)" onclick="selectKmdmTree(' + index + ')"><i class="fa fa-plus"></i>选择</a> ');
                        return actions.join('');
                    }
                },{
                    field: 'zrkmdm',
                    title: '转入科目代码',
                },{
                    field: 'zrkmmc',
                    title: '转入科目名称',
                },
                {
                    field: 'zrfx',
                    title: '转入方向',
		        	formatter: function (value, row, index) {
		        		return statuszrfx(row);
		        	}
                }]
            };
            $.table.init(options);
        });
        
    	function statusTools(row) {
		    if (row.kmlx =='-1') {
    			return '借';
    		} else {
    			return '贷';
    		}
		}
    	function statuszrfx(row) {
		    if (row.zrfx =='-1') {
    			return '借';
    		} else {
    			return '贷';
    		}
		}
        function doNzzz(){
        	var data = $("#" + table.options.id).bootstrapTable('getSelections');
        	var tbldata=$("#" + table.options.id).bootstrapTable('getData');
        	debugger;
        	$.modal.confirm("确认要转账吗？", function() {
		    	$.ajax({
					url:prefix + "/doNzzz", 
					data:JSON.stringify(tbldata),
					type:"post",
					headers: {'Content-Type': 'application/json;charset=utf8'},
					dataType:"json", 
					success:function(vdata){	
						if (vdata.code!='0'){
						  $.modal.alertError(vdata.msg);
						}else{
							 $.modal.alert("成功");	
						}
					},
					error:function(){
						$.modal.alertError("失败");
			        }
				});
        	})
         }
        
        /*会计科目-新增-选择父部门树*/
        function selectKmdmTree(upIndex) {
            var options = {
            	title: '会计科目'+'_'+upIndex,
                width: "380",
                url: ctx + "system/kmdm/selectKmdmTree/",
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
        	   var titile=$(".layui-layer-title")[0].innerText;
	     	   var rowIndex=0;
	     	   if (titile!=undefined){
	     		   rowIndex=titile.split('_')[1];
	     	   }
               var body = layer.getChildFrame('body', index);
               var kmdm=body.find('#treeId').val();
               var kmdmName=body.find('#treeTitle').val();
               var rowskmdm = {
            		   index : rowIndex, //更新列所在行的索引
            		   field : "zrkmdm", //要更新列的field
            		   value : kmdm //要更新列的数据
            		   }//更新表格数据 
               $("#" + table.options.id).bootstrapTable("updateCell",rowskmdm);
         	   var kmdmName = {
                 		   index : rowIndex, //更新列所在行的索引
                 		   field : "zrkmmc", //要更新列的field
                 		   value : kmdmName //要更新列的数据
                 		   }//更新表格数据 
               $("#" + table.options.id).bootstrapTable("updateCell",kmdmName);
               layer.close(index);
        }
    	
    </script>
</body>
</html>